package com.brillilab.service.logic.kit;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.brillilab.domain.po.system.Company;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.utils.MyBeanUtils;
import com.brillilab.domain.enums.kit.KitEnum;
import com.brillilab.domain.enums.kit.KitEnum.Classify;
import com.brillilab.domain.pa.kit.KitReagentSpecAddPa;
import com.brillilab.domain.pa.kit.KitSpecAddPa;
import com.brillilab.domain.pa.kit.SampleAddPa;
import com.brillilab.domain.pa.kit.SampleAttrPa;
import com.brillilab.domain.po.global.GlobalKit;
import com.brillilab.domain.po.global.GlobalKitSpec;
import com.brillilab.domain.po.global.GlobalReagent;
import com.brillilab.domain.po.global.GlobalReagentAntibody;
import com.brillilab.domain.po.global.GlobalReagentPlasmid;
import com.brillilab.domain.po.global.GlobalReagentPlasmidFragment;
import com.brillilab.domain.po.global.GlobalReagentPrimer;
import com.brillilab.domain.po.global.GlobalReagentPrimerAttr;
import com.brillilab.domain.po.global.GlobalSampleAttr;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.po.kit.KitSpec;
import com.brillilab.domain.po.kit.Reagent;
import com.brillilab.domain.po.kit.ReagentAntibody;
import com.brillilab.domain.po.kit.ReagentPlasmid;
import com.brillilab.domain.po.kit.ReagentPlasmidFragment;
import com.brillilab.domain.po.kit.ReagentPrimer;
import com.brillilab.domain.po.kit.ReagentPrimerAttr;
import com.brillilab.domain.po.kit.SampleTemplate;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.vo.kit.ReagentDetailsVo;
import com.brillilab.domain.vo.kit.ReagentInfoVo;
import com.brillilab.domain.vo.kit.ReagentKitSpecVo;
import com.brillilab.domain.vo.kit.ReagentSpecVo;
import com.brillilab.domain.vo.kit.SampleInfoVo;
import com.brillilab.domain.vo.kit.SampleSpecVo;
import com.brillilab.domain.vo.kit.SampleTemplateGroupVo;
import com.brillilab.service.constant.ServiceConstant;
import com.brillilab.service.core.elsearch.IEsKitSaveService;
import com.brillilab.service.core.global.IGlobalKitService;
import com.brillilab.service.core.global.IGlobalKitSpecService;
import com.brillilab.service.core.global.IGlobalReagentService;
import com.brillilab.service.core.kit.IKitService;
import com.brillilab.service.core.kit.IKitSpecService;
import com.brillilab.service.core.kit.IReagentAntibodyService;
import com.brillilab.service.core.kit.IReagentPlasmidFragmentService;
import com.brillilab.service.core.kit.IReagentPlasmidService;
import com.brillilab.service.core.kit.IReagentPrimerAttrService;
import com.brillilab.service.core.kit.IReagentPrimerService;
import com.brillilab.service.core.kit.IReagentService;
import com.brillilab.service.core.kit.IReagentTypeService;
import com.brillilab.service.core.kit.ISampleTemplateService;
import com.brillilab.service.core.system.IDictService;
import com.brillilab.service.core.system.IFileManageService;

import cn.hutool.core.collection.CollUtil;

/**
 * 全局试剂
 *
 * @author lj
 */
@Service
@Transactional
public class GlobalKitLogic {
	@Autowired
	private IGlobalKitService globalKitService;
	@Autowired
	private IGlobalReagentService globalReagentService;
	@Autowired
	private IGlobalKitSpecService globalKitSpecService;

	@Resource
	private IReagentTypeService reagentTypeService;
	@Autowired
	private IKitService kitService;
	@Autowired
	private IKitSpecService kitSpecService;
	@Autowired
	private IDictService dictService;
	@Autowired
	private IReagentService reagentService;
	@Autowired
	private IReagentAntibodyService reagentAntibodyService;
	@Autowired
	private IReagentPrimerService reagentPrimerService;
	@Autowired
	private IReagentPrimerAttrService reagentPrimerAttrService;
	@Autowired
	private IReagentPlasmidService reagentPlasmidService;
	@Autowired
	private IReagentPlasmidFragmentService reagentPlasmidFragmentService;
	@Autowired
	private ISampleTemplateService sampleTemplateService;
	@Autowired
	private SampleLogic sampleLogic;
	@Autowired
	private IFileManageService fileManageService;

	@Autowired
	private IEsKitSaveService esKitSaveService;

	/**
	 * 获取全局试剂详情
	 *
	 * @param kitId
	 * @return
	 */
	public ReagentDetailsVo getReagentDetails(Long kitId) {
		ReagentDetailsVo vo = null;
		GlobalKit kit = globalKitService.selectById(kitId);
		if (kit != null) {
			Map<Integer, String> unitMap = dictService.getUnitMap();
			Map<Integer, String> zoneMap = dictService.getZoneMap();
			Map<Long, String> reagentTypeMap = dictService.getReagentTypeMap(null);

			vo = MyBeanUtils.copyBean(kit, ReagentDetailsVo.class);
			vo.setIsLabExist(0);
			vo.setKitId(kit.getId());
			vo.setReagentTypeName(reagentTypeMap.get(kit.getReagentTypeId()));
			vo.setUnitName(unitMap.get(kit.getUnit()));
			vo.setZoneName(zoneMap.get(kit.getZone()));
			List<GlobalKitSpec> kitSpecList = globalKitSpecService.selectSpecGroup(kit.getId());
			String kitSpecName = globalKitSpecService.getSpecName(kitSpecList, 0);
			vo.setSpecName(kitSpecName);
			// 获取文件对象列表
			vo.setInstructionFileList(fileManageService.getFileList(kit.getInstruction()));
			List<ReagentInfoVo> reagentListVo = new ArrayList<>();
			List<GlobalReagent> reagentList = globalReagentService.selectList(kitId);

			List<Long> reagentTypeIds = reagentList.stream().map(GlobalReagent::getReagentTypeId)
					.collect(Collectors.toList());
			reagentTypeIds.add(kit.getReagentTypeId());
			Map<Long, Long> firstReagentTypeMap = reagentTypeService.selectFirstReagentTypeMap(reagentTypeIds);
			vo.setFirstReagentTypeId(firstReagentTypeMap.get(kit.getReagentTypeId()));

			for (GlobalReagent reagent : reagentList) {
				// 组装reagent信息数据
				ReagentInfoVo reagentVo = MyBeanUtils.copyBean(reagent, ReagentInfoVo.class);
				reagentVo.setIsLabExist(0);
				reagentVo.setReagentId(reagent.getId());
				reagentVo.setZoneName(zoneMap.get(reagent.getZone()));
				reagentVo.setReagentTypeName(reagentTypeMap.get(reagent.getReagentTypeId()));
				reagentVo.setUnitName(unitMap.get(reagent.getUnit()));
				reagentVo.setNumeratorUnitName(unitMap.get(reagent.getNumeratorUnit()));
				reagentVo.setDenominatorUnitName(unitMap.get(reagent.getDenominatorUnit()));
				reagentVo.setFirstReagentTypeId(firstReagentTypeMap.get(reagent.getReagentTypeId()));

				// 组装reagent规格
				List<GlobalKitSpec> specList = globalKitSpecService.selectSpecList(kit.getId(), reagent.getId());
				String specName = globalKitSpecService.getSpecName(specList, 1);
				reagentVo.setSpecName(specName);

				if (KitEnum.KitType.ONE.getValue().equals(kit.getKitType())) {
					GlobalKitSpec spec = specList.get(0);
					if (spec != null) {
						vo.setSize(spec.getSize());
						vo.setMultiple(spec.getMultiple());
					}
					// 获取文件对象列表
					reagentVo.setInstructionFileList(fileManageService.getFileList(kit.getInstruction()));
				} else {
					// 获取文件对象列表
					reagentVo.setInstructionFileList(fileManageService.getFileList(reagentVo.getInstruction()));
				}

				// 组装属性信息
				if (reagent.getReagentTypeId().equals(ServiceConstant.antibody_reagent_type_id)) {
					GlobalReagentAntibody globalAntibody = globalReagentService.selectAntibody(reagent.getKitId(),
							reagent.getId());
					ReagentAntibody antibody = MyBeanUtils.copyBean(globalAntibody, ReagentAntibody.class);
					reagentVo.setReagentAntibody(antibody);
				} else if (reagent.getReagentTypeId().equals(ServiceConstant.primer_reagent_type_id)) {
					GlobalReagentPrimer globalPrimer = globalReagentService.selectPrimer(reagent.getKitId(),
							reagent.getId());
					ReagentPrimer reagentPrimer = MyBeanUtils.copyBean(globalPrimer, ReagentPrimer.class);
					reagentVo.setReagentPrimer(reagentPrimer);
				} else if (reagent.getReagentTypeId().equals(ServiceConstant.plasmid_reagent_type_id)) {
					GlobalReagentPlasmid globalPlasmid = globalReagentService.selectPlasmid(reagent.getKitId(),
							reagent.getId());
					ReagentPlasmid reagentPlasmid = MyBeanUtils.copyBean(globalPlasmid, ReagentPlasmid.class);
					reagentVo.setReagentPlasmid(reagentPlasmid);
				}
				reagentListVo.add(reagentVo);

			}
			vo.setReagentList(reagentListVo);
		}
		return vo;
	}

	/**
	 * 获取全局试剂规格
	 *
	 * @param kitId
	 * @return
	 */
	public List<ReagentKitSpecVo> getReagentSpec(Long kitId) {
		List<ReagentKitSpecVo> listVo = new ArrayList<>();
		Map<String, ReagentKitSpecVo> map = new HashMap<>();
		Map<Integer, String> unitMap = dictService.getUnitMap();
		Map<Integer, String> zoneMap = dictService.getZoneMap();
		Map<Long, String> reagentTypeMap = dictService.getReagentTypeMap(null);

		GlobalKit kit = globalKitService.selectById(kitId);
		List<GlobalKitSpec> specList = globalKitSpecService.selectList(kitId, null);
		List<GlobalReagent> reagentList = globalReagentService.selectList(kitId);
		Map<Long, List<GlobalReagent>> reagentMap = reagentList.stream()
				.collect(Collectors.groupingBy(GlobalReagent::getId));

		List<Long> reagentTypeIds = reagentList.stream().map(GlobalReagent::getReagentTypeId)
				.collect(Collectors.toList());
		reagentTypeIds.add(kit.getReagentTypeId());
		Map<Long, Long> firstReagentTypeMap = reagentTypeService.selectFirstReagentTypeMap(reagentTypeIds);

		specList.forEach(kitSpec -> {
			// 组装reagent信息数据
			GlobalReagent reagent = reagentMap.get(kitSpec.getReagentId()).get(0);
			ReagentSpecVo vo = new ReagentSpecVo();
			vo.setIsLabExist(0);
			vo.setSpectId(kitSpec.getId());
			vo.setKitId(reagent.getKitId());
			vo.setReagentId(reagent.getId());
			vo.setReagentName(reagent.getName());
			vo.setReagentTypeId(reagent.getReagentTypeId());
			vo.setReagentTypeName(reagentTypeMap.get(reagent.getReagentTypeId()));
			vo.setUnit(reagent.getUnit());
			vo.setUnitName(unitMap.get(reagent.getUnit()));
			vo.setZone(reagent.getZone());
			vo.setZoneName(zoneMap.get(reagent.getZone()));
			vo.setSize(kitSpec.getSize());
			vo.setMultiple(kitSpec.getMultiple());
			vo.setFirstReagentTypeId(firstReagentTypeMap.get(reagent.getReagentTypeId()));

			// 规格比率
			BigDecimal specRatio = kitSpec.getSize().multiply(BigDecimal.valueOf(kitSpec.getMultiple()))
					.divide(kitSpec.getKitSize(), 8, RoundingMode.HALF_UP);
			vo.setSpecRatio(specRatio);

			// 组装reagent规格名称
			List<GlobalKitSpec> list = new ArrayList<>();
			list.add(kitSpec);
			String reagentSpecName = globalKitSpecService.getSpecName(list, 1);
			vo.setSpecName(reagentSpecName);

			// 组装kit规格名称
			String kitSpecName = globalKitSpecService.getSpecName(list, 0);
			ReagentKitSpecVo rslVo = map.get(kitSpecName);
			if (rslVo == null) {
				// 组装kit信息数据
				rslVo = new ReagentKitSpecVo();
				rslVo.setIsLabExist(0);
				rslVo.setKitId(kitId);
				rslVo.setKitName(kit.getName());
				rslVo.setSpecName(kitSpecName);
				rslVo.setKitType(kit.getKitType());
				rslVo.setKitSize(kitSpec.getKitSize());
				rslVo.setUnit(kit.getUnit());
				rslVo.setUnitName(unitMap.get(kit.getUnit()));
				rslVo.setZoneName(zoneMap.get(kit.getZone()));
				rslVo.setFirstReagentTypeId(firstReagentTypeMap.get(kit.getReagentTypeId()));
				rslVo.setReagentList(new ArrayList<>());
			}
			rslVo.getReagentList().add(vo);
			map.put(kitSpecName, rslVo);

		});
		map.values().forEach(rslVo -> listVo.add(rslVo));
		return listVo;
	}

	/**
	 * 新增试剂规格
	 *
	 * @param pa
	 * @return
	 */
	public Long addReagentSpec(KitSpecAddPa pa) {
		GlobalKit globalKit = globalKitService.selectById(pa.getKitId());
		// 1、保存kit信息
		Kit kit = MyBeanUtils.copyBean(globalKit, Kit.class);
		kit.setId(null);
		kit.setLabId(pa.getLabId());
		kit.setFromId(globalKit.getId());
		kit.setCreatorId(pa.getCreatorId());
		kit = kitService.insert(kit);
		fileManageService.insertBatch(globalKit.getInstruction(), kit.getId(), OssFileEnum.KitInstructionFile,
				kit.getLabId(), null);
		List<GlobalReagent> globalReagentList = globalReagentService.selectList(globalKit.getId());

		for (GlobalReagent globalReagent : globalReagentList) {
			// 2、保存reagent信息
			Reagent reagent = MyBeanUtils.copyBean(globalReagent, Reagent.class);
			reagent.setId(null);
			reagent.setLabId(pa.getLabId());
			reagent.setKitId(kit.getId());
			reagent.setFromId(globalKit.getId());
			reagent = reagentService.insert(reagent);
			fileManageService.insertBatch(globalReagent.getInstruction(), reagent.getId(),
					OssFileEnum.ReagentInstructionFile, kit.getLabId(), null);
			// 3、保存新增规格信息
			List<KitSpec> saveSpecList = new ArrayList<KitSpec>();
			List<KitReagentSpecAddPa> reagentSpecList = pa.getKitReagentSpecList();
			Map<Long, List<KitReagentSpecAddPa>> specAddPaMap = reagentSpecList.stream()
					.collect(Collectors.groupingBy(KitReagentSpecAddPa::getReagentId));

			for (KitReagentSpecAddPa specAddPa : reagentSpecList) {
				if (specAddPa.getReagentId().equals(globalReagent.getId())) {
					Assert.isTrue(specAddPa.getSize() != null && specAddPa.getSize().compareTo(BigDecimal.ZERO) == 1,
							"试剂规格必须大于0");
					Assert.isTrue(specAddPa.getMultiple() != null && specAddPa.getMultiple() > 0, "试剂支数必须大于0");

					KitSpec kitSpec = new KitSpec();
					kitSpec.setLabId(pa.getLabId());
					kitSpec.setKitId(kit.getId());
					kitSpec.setReagentId(reagent.getId());
					if (globalKit.getKitType() == 0) {// 单成分
						kitSpec.setKitSize(specAddPa.getSize().multiply(BigDecimal.valueOf(specAddPa.getMultiple())));
					} else {// 多成分
						Assert.isTrue(pa.getKitSize() != null && pa.getKitSize().compareTo(BigDecimal.ZERO) == 1,
								"试剂规格必须大于0");
						kitSpec.setKitSize(pa.getKitSize());
					}
					kitSpec.setSize(specAddPa.getSize());
					kitSpec.setUnit(specAddPa.getUnit());
					kitSpec.setMultiple(specAddPa.getMultiple());
					saveSpecList.add(kitSpec);
					break;
				}
			}

			// 4、保存原有规格信息
			List<GlobalKitSpec> specList = globalKitSpecService.selectSpecList(globalKit.getId(),
					globalReagent.getId());
			for (GlobalKitSpec globalKitSpec : specList) {
				if (globalKitSpec.getKitSize() != null && globalKitSpec.getKitSize().compareTo(BigDecimal.ZERO) == 1
						&& globalKitSpec.getSize() != null && globalKitSpec.getSize().compareTo(BigDecimal.ZERO) == 1
						&& globalKitSpec.getMultiple() != null && globalKitSpec.getMultiple() > 0) {
					KitReagentSpecAddPa specAddPa = specAddPaMap.get(globalKitSpec.getReagentId()).get(0);
					BigDecimal paKitSize = specAddPa.getSize().multiply(BigDecimal.valueOf(specAddPa.getMultiple()));
					if (paKitSize.compareTo(globalKitSpec.getKitSize()) != 0) {
						KitSpec kitSpec = new KitSpec();
						kitSpec.setLabId(pa.getLabId());
						kitSpec.setKitId(kit.getId());
						kitSpec.setReagentId(reagent.getId());
						kitSpec.setKitSize(globalKitSpec.getKitSize());
						kitSpec.setSize(globalKitSpec.getSize());
						kitSpec.setUnit(globalKitSpec.getUnit());
						kitSpec.setMultiple(globalKitSpec.getMultiple());
						saveSpecList.add(kitSpec);
					}
				}
			}
			// 批量插入规格
			kitSpecService.insertBatch(saveSpecList);

			// 5、保存属性信息
			if (reagent.getReagentTypeId().equals(ServiceConstant.antibody_reagent_type_id)) {
				GlobalReagentAntibody globalAntibody = globalReagentService.selectAntibody(globalReagent.getKitId(),
						globalReagent.getId());
				if (globalAntibody != null) {
					ReagentAntibody antibody = MyBeanUtils.copyBean(globalAntibody, ReagentAntibody.class);
					antibody.setId(null);
					antibody.setKitId(reagent.getKitId());
					antibody.setReagentId(reagent.getId());
					reagentAntibodyService.insert(antibody);
				}
			} else if (reagent.getReagentTypeId().equals(ServiceConstant.primer_reagent_type_id)) {
				GlobalReagentPrimer globalPrimer = globalReagentService.selectPrimer(globalReagent.getKitId(),
						globalReagent.getId());
				if (globalPrimer != null) {
					ReagentPrimer reagentPrimer = MyBeanUtils.copyBean(globalPrimer, ReagentPrimer.class);
					reagentPrimer.setId(null);
					reagentPrimer.setKitId(reagent.getKitId());
					reagentPrimer.setReagentId(reagent.getId());
					reagentPrimerService.insert(reagentPrimer);
					List<GlobalReagentPrimerAttr> primerAttrList = globalPrimer.getPrimerAttrList();
					if (primerAttrList != null && primerAttrList.size() > 0) {
						for (GlobalReagentPrimerAttr globalPrimerAttr : primerAttrList) {
							ReagentPrimerAttr reagentPrimerAttr = MyBeanUtils.copyBean(globalPrimerAttr,
									ReagentPrimerAttr.class);
							reagentPrimerAttr.setId(null);
							reagentPrimerAttr.setKitId(reagent.getKitId());
							reagentPrimerAttr.setReagentId(reagent.getId());
							reagentPrimerAttrService.insert(reagentPrimerAttr);
						}
					}
				}
			} else if (reagent.getReagentTypeId().equals(ServiceConstant.plasmid_reagent_type_id)) {
				GlobalReagentPlasmid globalPlasmid = globalReagentService.selectPlasmid(globalReagent.getKitId(),
						globalReagent.getId());
				if (globalPlasmid != null) {
					ReagentPlasmid reagentPlasmid = MyBeanUtils.copyBean(globalPlasmid, ReagentPlasmid.class);
					reagentPlasmid.setId(null);
					reagentPlasmid.setLabId(reagent.getLabId());
					reagentPlasmid.setKitId(reagent.getKitId());
					reagentPlasmid.setReagentId(reagent.getId());
					reagentPlasmid = reagentPlasmidService.insert(reagentPlasmid);
					// 保存附件
					fileManageService.insertBatch(globalPlasmid.getAbstractFile(), globalPlasmid.getId(),
							OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
					fileManageService.insertBatch(globalPlasmid.getAtlasFile(), globalPlasmid.getId(),
							OssFileEnum.PlasmidAtlasFile, kit.getLabId(), null);
					fileManageService.insertBatch(globalPlasmid.getReportFile(), globalPlasmid.getId(),
							OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
					fileManageService.insertBatch(globalPlasmid.getOtherFile(), globalPlasmid.getId(),
							OssFileEnum.PlasmidOtherFile, kit.getLabId(), null);

					List<GlobalReagentPlasmidFragment> fragmentList = globalPlasmid.getFragmentList();
					if (fragmentList != null && fragmentList.size() > 0) {
						for (GlobalReagentPlasmidFragment globalFragment : fragmentList) {
							ReagentPlasmidFragment reagentPlasmidFragment = MyBeanUtils.copyBean(globalFragment,
									ReagentPlasmidFragment.class);
							reagentPlasmidFragment.setId(null);
							reagentPlasmidFragment.setKitId(reagent.getKitId());
							reagentPlasmidFragment.setReagentId(reagent.getId());
							reagentPlasmidFragmentService.insert(reagentPlasmidFragment);
						}
					}
				}
			}
		}
		esKitSaveService.kitSave(kit.getId());
		return kit.getId();
	}

	/**
	 * 导入为新试剂
	 *
	 * @param labId
	 * @param globalKitId
	 * @return
	 */
	public Long reagentImport(Long labId, Long globalKitId, Long creatorId) {
		List<Kit> kitList = kitService.selectListByFromId(labId, globalKitId);
		if (CollUtil.isNotEmpty(kitList)) {
			return kitList.get(0).getId();
		}
		GlobalKit globalKit = globalKitService.selectById(globalKitId);
		// 1、保存kit信息
		Kit kit = MyBeanUtils.copyBean(globalKit, Kit.class);
		kit.setId(null);
		kit.setLabId(labId);
		kit.setFromId(globalKit.getId());
		kit.setCreatorId(creatorId);
		kit = kitService.insert(kit);
		fileManageService.insertBatch(globalKit.getInstruction(), kit.getId(), OssFileEnum.KitInstructionFile, labId,
				null);
		List<GlobalReagent> globalReagentList = globalReagentService.selectList(globalKit.getId());

		for (GlobalReagent globalReagent : globalReagentList) {
			// 2、保存reagent信息
			Reagent reagent = MyBeanUtils.copyBean(globalReagent, Reagent.class);
			reagent.setId(null);
			reagent.setLabId(labId);
			reagent.setKitId(kit.getId());
			reagent.setFromId(globalKit.getId());
			reagent = reagentService.insert(reagent);
			fileManageService.insertBatch(globalReagent.getInstruction(), reagent.getId(),
					OssFileEnum.ReagentInstructionFile, labId, null);
			// 3、保存原有规格信息
			List<KitSpec> saveSpecList = new ArrayList<KitSpec>();
			List<GlobalKitSpec> specList = globalKitSpecService.selectSpecList(globalKit.getId(),
					globalReagent.getId());
			for (GlobalKitSpec globalKitSpec : specList) {
				if (globalKitSpec.getKitSize() != null && globalKitSpec.getKitSize().compareTo(BigDecimal.ZERO) == 1
						&& globalKitSpec.getSize() != null && globalKitSpec.getSize().compareTo(BigDecimal.ZERO) == 1
						&& globalKitSpec.getMultiple() != null && globalKitSpec.getMultiple() > 0) {
					KitSpec kitSpec = new KitSpec();
					kitSpec.setLabId(labId);
					kitSpec.setKitId(kit.getId());
					kitSpec.setReagentId(reagent.getId());
					kitSpec.setUnit(globalKitSpec.getUnit());
					kitSpec.setKitSize(globalKitSpec.getKitSize());
					kitSpec.setSize(globalKitSpec.getSize());
					kitSpec.setMultiple(globalKitSpec.getMultiple());
					saveSpecList.add(kitSpec);
				}
			}
			// 批量插入规格
			kitSpecService.insertBatch(saveSpecList);

			// 5、保存属性信息
			if (reagent.getReagentTypeId().equals(ServiceConstant.antibody_reagent_type_id)) {
				GlobalReagentAntibody globalAntibody = globalReagentService.selectAntibody(globalReagent.getKitId(),
						globalReagent.getId());
				if (globalAntibody != null) {
					ReagentAntibody antibody = MyBeanUtils.copyBean(globalAntibody, ReagentAntibody.class);
					antibody.setId(null);
					antibody.setKitId(reagent.getKitId());
					antibody.setReagentId(reagent.getId());
					reagentAntibodyService.insert(antibody);
				}
			} else if (reagent.getReagentTypeId().equals(ServiceConstant.primer_reagent_type_id)) {
				GlobalReagentPrimer globalPrimer = globalReagentService.selectPrimer(globalReagent.getKitId(),
						globalReagent.getId());
				if (globalPrimer != null) {
					ReagentPrimer reagentPrimer = MyBeanUtils.copyBean(globalPrimer, ReagentPrimer.class);
					reagentPrimer.setId(null);
					reagentPrimer.setKitId(reagent.getKitId());
					reagentPrimer.setReagentId(reagent.getId());
					reagentPrimerService.insert(reagentPrimer);
					List<GlobalReagentPrimerAttr> primerAttrList = globalPrimer.getPrimerAttrList();
					if (primerAttrList != null && primerAttrList.size() > 0) {
						for (GlobalReagentPrimerAttr globalPrimerAttr : primerAttrList) {
							ReagentPrimerAttr reagentPrimerAttr = MyBeanUtils.copyBean(globalPrimerAttr,
									ReagentPrimerAttr.class);
							reagentPrimerAttr.setId(null);
							reagentPrimerAttr.setKitId(reagent.getKitId());
							reagentPrimerAttr.setReagentId(reagent.getId());
							reagentPrimerAttrService.insert(reagentPrimerAttr);
						}
					}
				}
			} else if (reagent.getReagentTypeId().equals(ServiceConstant.plasmid_reagent_type_id)) {
				GlobalReagentPlasmid globalPlasmid = globalReagentService.selectPlasmid(globalReagent.getKitId(),
						globalReagent.getId());
				if (globalPlasmid != null) {
					ReagentPlasmid reagentPlasmid = MyBeanUtils.copyBean(globalPlasmid, ReagentPlasmid.class);
					reagentPlasmid.setId(null);
					reagentPlasmid.setLabId(reagent.getLabId());
					reagentPlasmid.setKitId(reagent.getKitId());
					reagentPlasmid.setReagentId(reagent.getId());
					// 保存附件
					reagentPlasmid = reagentPlasmidService.insert(reagentPlasmid);
					// 保存附件
					fileManageService.insertBatch(globalPlasmid.getAbstractFile(), globalPlasmid.getId(),
							OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
					fileManageService.insertBatch(globalPlasmid.getAtlasFile(), globalPlasmid.getId(),
							OssFileEnum.PlasmidAtlasFile, kit.getLabId(), null);
					fileManageService.insertBatch(globalPlasmid.getReportFile(), globalPlasmid.getId(),
							OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
					fileManageService.insertBatch(globalPlasmid.getOtherFile(), globalPlasmid.getId(),
							OssFileEnum.PlasmidOtherFile, kit.getLabId(), null);

					List<GlobalReagentPlasmidFragment> fragmentList = globalPlasmid.getFragmentList();
					if (fragmentList != null && fragmentList.size() > 0) {
						for (GlobalReagentPlasmidFragment globalFragment : fragmentList) {
							ReagentPlasmidFragment reagentPlasmidFragment = MyBeanUtils.copyBean(globalFragment,
									ReagentPlasmidFragment.class);
							reagentPlasmidFragment.setId(null);
							reagentPlasmidFragment.setKitId(reagent.getKitId());
							reagentPlasmidFragment.setReagentId(reagent.getId());
							reagentPlasmidFragmentService.insert(reagentPlasmidFragment);
						}
					}
				}
			}
		}
		esKitSaveService.kitSave(kit.getId());
		return kit.getId();
	}

	/**
	 * 获取样品信息详情
	 *
	 * @param kitId kitD
	 * @return
	 */
	public SampleInfoVo getSampleDetails(Long kitId) {

		GlobalKit kit = globalKitService.selectById(kitId);
		Assert.isTrue(Classify.SAMPLE.getValue().equals(kit.getClassify()),
				ResultEnum.REQUEST_PARAM_ERROR.getMessage());

		Map<Integer, String> unitMap = dictService.getUnitMap();
		Map<Integer, String> zoneMap = dictService.getZoneMap();
		Map<Long, String> reagentTypeMap = dictService.getReagentTypeMap(null);
		SampleInfoVo vo = MyBeanUtils.copyBean(kit, SampleInfoVo.class);
		vo.setIsLabExist(0);
		vo.setKitId(kit.getId());

		vo.setReagentTypeName(reagentTypeMap.get(kit.getReagentTypeId()));
		vo.setUnitName(unitMap.get(kit.getUnit()));
		vo.setZoneName(zoneMap.get(kit.getZone()));

		List<FileManage> fileList = fileManageService.getFileList(kit.getInstruction());
		vo.setFileList(fileList);

		// 组装规格
		List<GlobalKitSpec> specList = globalKitSpecService.selectSpecGroup(kit.getId());
		GlobalKitSpec kitSpec = specList.get(0);
		vo.setKitSpecId(kitSpec.getId());
		vo.setReagentId(kitSpec.getReagentId());
		vo.setSize(kitSpec.getSize());
		vo.setMultiple(kitSpec.getMultiple());
		String specName = globalKitSpecService.getSpecName(specList, 1);
		vo.setSpecName(specName);
		// 组装属性值
		List<SampleTemplateGroupVo> groupList = sampleTemplateService.getSampleTemplateGroup(kit.getReagentTypeId(),
				null, null);
		List<GlobalSampleAttr> attrList = globalReagentService.selectSampleAttrList(kitId, vo.getReagentId());

		if (attrList != null && attrList.size() > 0) {
			Map<Long, List<GlobalSampleAttr>> attrMapp = attrList.stream()
					.collect(Collectors.groupingBy(GlobalSampleAttr::getSampleTemplateId));
			groupList.forEach(groupVo -> {
				List<SampleTemplate> templateList = groupVo.getTemplateList();
				if (templateList != null && templateList.size() > 0) {
					templateList.forEach(template -> {
						List<GlobalSampleAttr> list = attrMapp.get(template.getId());
						if (list != null && list.size() > 0) {
							GlobalSampleAttr attr = list.get(0);
							template.setSampleAttrId(attr.getId());
							template.setAttrValue(attr.getAttrValue());
						}
					});
				}
			});
		}
		vo.setSampleAttrList(groupList);
		return vo;
	}

	/**
	 * 样品导入
	 *
	 * @param labId
	 * @param globalKitId
	 * @return
	 */
	public SampleSpecVo sampleImport(Long labId, Long globalKitId, Long creatorId) {
		SampleInfoVo sampleInfoVo = this.getSampleDetails(globalKitId);
		SampleAddPa pa = MyBeanUtils.copyBean(sampleInfoVo, SampleAddPa.class);
		pa.setCreatorId(creatorId);
		pa.setLabId(labId);
		pa.setFromId(globalKitId);
		List<SampleAttrPa> sampleAttrList = new ArrayList<>();
		List<SampleTemplateGroupVo> groupAttrList = sampleInfoVo.getSampleAttrList();
		if (groupAttrList != null && groupAttrList.size() > 0) {
			groupAttrList.forEach(group -> {
				List<SampleTemplate> templateList = group.getTemplateList();
				if (templateList != null && templateList.size() > 0) {
					templateList.forEach(template -> {
						if (StringUtils.isNotBlank(template.getAttrValue())) {
							SampleAttrPa attrPa = new SampleAttrPa();
							attrPa.setSampleTemplateId(template.getId());
							attrPa.setAttrName(template.getAttrName());
							attrPa.setAttrValue(template.getAttrValue());
							sampleAttrList.add(attrPa);
						}
					});
				}
			});
		}
		pa.setSampleAttrList(sampleAttrList);
		return sampleLogic.addSampleOne(pa);
	}

	public List<Company> searchCompanyByReagentType(Long reagentTypeId) {
		// List<GlobalKit> list =
		// globalKitService.searchCompanyByReagentType(reagentTypeId);
		List<Company> companyList = new ArrayList<>();
		// list.forEach(g->{
		// Company c = new Company();
		// c.setId(g.getCompanyId());
		// c.setName(g.getCompany());
		// companyList.add(c);
		// });
		Company c = new Company();
		c.setId(17L);
		c.setName("BioLegend");
		companyList.add(c);

		Company c1 = new Company();
		c1.setId(19L);
		c1.setName("Bio-Rad");
		companyList.add(c1);

		Company c2 = new Company();
		c2.setId(21L);
		c2.setName("BioVision");
		companyList.add(c2);

		Company c3 = new Company();
		c3.setId(27L);
		c3.setName("CST");
		companyList.add(c3);

		Company c4 = new Company();
		c4.setId(45L);
		c4.setName("Miltenyi Biotec");
		companyList.add(c4);

		Company c5 = new Company();
		c5.setId(47L);
		c5.setName("NEB");
		companyList.add(c5);

		Company c6 = new Company();
		c6.setId(55L);
		c6.setName("R&D Systems");
		companyList.add(c6);

		Company c7 = new Company();
		c7.setId(60L);
		c7.setName("STEMCELL Technologies");
		companyList.add(c7);

		Company c8 = new Company();
		c8.setId(70L);
		c8.setName("Abcam");
		companyList.add(c8);

		// Company c9 = new Company();
		// c9.setId(71L);
		// c9.setName("");
		// companyList.add(c9);

		Company c11 = new Company();
		c11.setId(102L);
		c11.setName("InvivoGen");
		companyList.add(c11);

		Company c22 = new Company();
		c22.setId(110L);
		c22.setName("Novus");
		companyList.add(c22);

		Company c33 = new Company();
		c33.setId(117L);
		c33.setName("Santa Cruz");
		companyList.add(c33);

		Company c44 = new Company();
		c44.setId(162L);
		c44.setName("promega");
		companyList.add(c44);

		Company c55 = new Company();
		c55.setId(214L);
		c55.setName("Active Motif");
		companyList.add(c55);

		Company c66 = new Company();
		c66.setId(215L);
		c66.setName("BD");
		companyList.add(c66);

		Company c00 = new Company();
		c00.setId(216L);
		c00.setName("Merck Millipore");
		companyList.add(c00);

		Company c77 = new Company();
		c77.setId(217L);
		c77.setName("Thermo Fisher");
		companyList.add(c77);

		Company c88 = new Company();
		c88.setId(222L);
		c88.setName("Sigma-Aldrich");
		companyList.add(c88);

		Company c99 = new Company();
		c99.setId(225L);
		c99.setName("Polyplus");
		companyList.add(c99);

		Company c991 = new Company();
		c991.setId(227L);
		c991.setName("AXYGEN");
		companyList.add(c991);

		Company c992 = new Company();
		c992.setId(242L);
		c992.setName("Novus Biologicals");
		companyList.add(c992);
		return companyList;
	}
}
